// 堆+贪心;时间复杂度O((n+k)logn);空间复杂度O(n)

class Solution {
public:
    int findMaximizedCapital(int k, int w, vector<int>& profits, vector<int>& capital) {
        int n = profits.size();
        priority_queue<int, vector<int>, less<int>> pq;
        vector<vector<int>> vvi;
        for (int i = 0; i < n; i++) {
            vvi.push_back({capital[i], profits[i]});
        }
        sort(vvi.begin(), vvi.end());
        int pos = 0;
        while (k > 0) {
            while (pos < n && vvi[pos][0] <= w) {
                pq.push(vvi[pos][1]);
                pos++;
            }
            if (pq.empty()) break;
            w += pq.top();
            pq.pop();
            k--;
        }
        return w;
    }
};